﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Htss.Platform.Web.Http;
using System.Web.Routing;

namespace DreamBox.Web
{
    public class Global : System.Web.HttpApplication
    {

        private void RegisterRoute(RouteCollection routeCollection)
        {
            routeCollection.MapRouteBox("route4", "user/{box}",
                new { box = "portal" },"user");

            routeCollection.MapRouteBox("route5", "talk/{box}",
                new { box = "portal" }, "talk");

            routeCollection.MapRouteBox("route6", "talk/{box}/{id}",
                new { box = "portal" }, "talk");

            routeCollection.MapRouteBox("route3", "posts/{box}",
                new { box = "portal" }, "posts");

            routeCollection.MapRouteBox("route7", "posts/{box}/{id}",
                new { box = "portal" }, "posts");

            routeCollection.MapRouteBox("route2", "{box}/{id}",
                new { box = "portal", id = 0 });

            routeCollection.MapRouteBox("route1", "{box}",

                new { box = "portal" });

            routeCollection.MapRouteBox("default", "",
                new { box = "portal" });

        }
        protected void Application_Start(object sender, EventArgs e)
        {
            //注册路由
            RegisterRoute(RouteTable.Routes);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                HttpCookie cookie = Request.Cookies["dbox_user"];
                BLL.User.Competence.ComGroupRight group = new BLL.User.Competence.ComGroupRight();
                if (cookie != null)
                {
                    DAL.User.Login login = new DAL.User.Login();
                    DataTable dt = login.UserLogin(cookie.Values["username"].ToString(), cookie.Values["password"].ToString());
                    if (dt.Rows.Count > 0)
                    {
                        Session["UserID"] = dt.Rows[0]["UserID"];
                        Session["UserName"] = dt.Rows[0]["UserName"];
                        Session["Avatar"] = dt.Rows[0]["Avatar"];
                        Session["Location"] = dt.Rows[0]["Location"];
                        //登录后的权限记录
                        group.SetCompetence(HttpContext.Current, dt);
                    }
                    else
                    {
                        SetCompetence(group);
                    }
                }
                else
                {
                    SetCompetence(group);
                }
            }
        }
        public void SetCompetence(BLL.User.Competence.ComGroupRight group)
        {
            //获取默认组的信息
            DataTable dt = group.GetDefaultCompetence();
            group.SetCompetence(HttpContext.Current, dt);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}